package com.prefboss.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/*import com.prefboss.exception.BussinessException;
import com.prefboss.exception.ExceptionEnums;
import com.prefboss.interceptor.TenantContext;*/
import com.prefboss.job.dao.XxlJobInfoMapper;
import com.prefboss.job.entity.XxlJobGroup;
import com.prefboss.job.entity.XxlJobInfo;
import com.prefboss.job.service.XxlJobInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类，动态指定任务并创建和修改任务的属性，调度中心会根据这个来执行对应的job
 * 根据appId 和 tenantId来识别用户
 * </p>
 *
 * @author XuJiangbo
 * @since 2020-06-16
 */
@Service("xxlJobInfoServiceImpl")
public class XxlJobInfoServiceImpl extends ServiceImpl<XxlJobInfoMapper, XxlJobInfo> implements XxlJobInfoService {
    @Autowired
    private XxlJobGroupServiceImpl xxlJobGroupService;

    @Value("${xxl.job.executor.appname}")
    private String executorName;

    @Override
    public void add(Integer clearRule) {
        String cron = "";
        switch (clearRule) {
            case 1:
            case 2:
//                cron = "0 0 0 1 /1 ? *";//月清
                cron = "0/10 * * * * ? ";//测试用
                break;
            case 3:
                cron = "0 0 0 1 /3 ? *";//季清
                break;
            case 4:
                cron = "0 0 0 1 /6 ? *";//半年清
                break;
            case 5:
                cron = "0 0 0 1 /12 ? *";//年清
                break;
        }
        if (cron.equals("")) {
//            throw new BussinessException(ExceptionEnums.OTHER_ERROR, "规则选项超过范围");
        }
        XxlJobInfo xxlJobInfo = new XxlJobInfo();
        //从上下文中获取当前线程对应的用户信息，在多租户获取中已经设置进去
        String tenantId;/* = TenantContext.getTenantId(); //多租户的全局变量*/
        String appId ;/* = TenantContext.getAppId();*/
        tenantId = "1";
        appId = "1";
        XxlJobGroup xxlJobGroup = xxlJobGroupService.getOne(new QueryWrapper<XxlJobGroup>().lambda().eq(XxlJobGroup::getAppName, executorName));
        if (xxlJobGroup == null || "".equals(xxlJobGroup.getAddressList())) {
//            throw new BussinessException(ExceptionEnums.OTHER_ERROR, "无执行器错误");
        }
        //指定执行器，一般指定本模块对应的执行器
        xxlJobInfo.setJobGroup(xxlJobGroup.getId());
        xxlJobInfo.setJobCron(cron);
        xxlJobInfo.setJobDesc(appId + "-" + tenantId + "-" + clearRule);
        xxlJobInfo.setAddTime(LocalDateTime.now());
        xxlJobInfo.setUpdateTime(LocalDateTime.now());
        xxlJobInfo.setAuthor("XXL");
        xxlJobInfo.setExecutorRouteStrategy("FIRST");
        xxlJobInfo.setExecutorHandler("clearRuleHandler");//执行的任务
        xxlJobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
        xxlJobInfo.setExecutorTimeout(1);
        xxlJobInfo.setExecutorFailRetryCount(1);
        xxlJobInfo.setGlueType("BEAN");
        xxlJobInfo.setGlueUpdatetime(LocalDateTime.now());
        if (clearRule == 1) {
            xxlJobInfo.setTriggerStatus(0);
        } else {
            xxlJobInfo.setTriggerStatus(1);
        }
        xxlJobInfo.setExecutorParam(appId + "," + tenantId);
        xxlJobInfo.setTenantId(tenantId);
        xxlJobInfo.setAppId(appId);
        //根据 appId和 tenantId 来保存或更新
        this.saveOrUpdate(xxlJobInfo, new UpdateWrapper<XxlJobInfo>().lambda().eq(XxlJobInfo::getAppId, appId).eq(XxlJobInfo::getTenantId, tenantId));
    }
}
